package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.config.RedisKey;
import com.qf.dao.DrugMapper;
import com.qf.pojo.entity.MedicleDrug;
import com.qf.pojo.req.ReqDrug;
import com.qf.pojo.resp.Resultmsg;
import com.qf.service.DrugService;
import com.qf.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DrugServiceImpl
 * @Description
 * @Author lanzhike
 * @Date 2023/8/14/0014 上午 10:49
 * @Version 1.0
 */
@Service
public class DrugServiceImpl implements DrugService {

    @Autowired
    RedisTemplate redisTemplate;
    @Resource
    DrugMapper drugMapper;
    @Override
    public Resultmsg addShopCat(ReqDrug reqDrug, HttpServletRequest httpServletRequest) {

//        String str = JSONObject.toJSONString(medicleDrug);
        String userId = UserUtil.getUserId(httpServletRequest);
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.SHOPCART_KEY + userId);
        Boolean bBoolean = redisTemplate.hasKey(RedisKey.SHOPCART_NUM + userId);
        if (!aBoolean || !bBoolean){
            QueryWrapper<MedicleDrug> wrapper = new QueryWrapper<>();
            wrapper.eq("drug_id", reqDrug.getDrugId());
            MedicleDrug medicleDrug = drugMapper.selectOne(wrapper);
            if(medicleDrug == null){
                return Resultmsg.fail("该药品已售罄");
            }
            //没有购物车,创建
            redisTemplate.opsForHash().put(RedisKey.SHOPCART_KEY + userId,medicleDrug.getDrugId().toString(),medicleDrug);
            redisTemplate.opsForHash().put(RedisKey.SHOPCART_NUM+userId,medicleDrug.getDrugId().toString(),reqDrug.getDrugNum());
            return Resultmsg.success();
        }
        Boolean Boolean = redisTemplate.boundHashOps(RedisKey.SHOPCART_KEY + userId).hasKey(reqDrug.getDrugId().toString());

        if (Boolean){
            //有购物车,有对应的商品
            Object o = redisTemplate.boundHashOps(RedisKey.SHOPCART_NUM + userId).get(reqDrug.getDrugId().toString());
            int num = Integer.parseInt(String.valueOf(o));
            redisTemplate.boundHashOps(RedisKey.SHOPCART_NUM+userId).put(reqDrug.getDrugId().toString(),num+reqDrug.getDrugNum());
            return Resultmsg.success();
        }else {
            //有购物车,无对应商品
            QueryWrapper<MedicleDrug> wrapper = new QueryWrapper<>();
            wrapper.eq("drug_id", reqDrug.getDrugId());
            MedicleDrug medicleDrug = drugMapper.selectOne(wrapper);
            if(medicleDrug == null){
                return Resultmsg.fail("该药品已售罄");
            }
            redisTemplate.opsForHash().put(RedisKey.SHOPCART_KEY + userId,medicleDrug.getDrugId().toString(),medicleDrug);
            redisTemplate.boundHashOps(RedisKey.SHOPCART_NUM+userId).put(medicleDrug.getDrugId().toString(),reqDrug.getDrugNum());
            return Resultmsg.success();
        }
    }

    @Override
    public Resultmsg getDrugCat(HttpServletRequest httpServletRequest) {
        String userId = UserUtil.getUserId(httpServletRequest);
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.SHOPCART_KEY + userId);
        if (!aBoolean){
            return Resultmsg.fail("购物车空空如也");
        }
        List drugs = redisTemplate.boundHashOps(RedisKey.SHOPCART_KEY + userId).values();
        List drugNums = redisTemplate.boundHashOps(RedisKey.SHOPCART_NUM + userId).values();


        Map drugMap = new HashMap<MedicleDrug,Integer>();
        for (int i = 0; i < drugs.size(); i++) {
            drugMap.put(drugs.get(i),drugNums.get(i));
        }
        System.out.println(drugMap);
        return Resultmsg.success(drugMap);
    }
}
